package com.example.bensonzheng.appcompattest;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.readystatesoftware.systembartint.SystemBarTintManager;

/**
 * 作者： benson.zheng   时间： 15/9/10.
 * 功能：
 * 修改：
 */
public class Utils {
    public static int sStatusHeight;

    public static int getStatusBarHeight(Activity mActivity) {
        SystemBarTintManager sb = new SystemBarTintManager(mActivity);
        return sb.getConfig().getStatusBarHeight();
    }


    @SuppressLint("NewApi")
    public static void setMaterialDesignMode(Activity context, boolean isUseCustom, int color) {

        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT && isUseCustom) {
            setTranslucentStatus(context, true);
        }
        if (Build.VERSION.SDK_INT >= 19 && isUseCustom) {
            context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        color = isUseCustom ? Color.TRANSPARENT : color;
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {

            SystemBarTintManager tintManager = new SystemBarTintManager(context);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintColor(color);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            context.getWindow().setStatusBarColor(color);
        }
    }

    @TargetApi(19)
    private static void setTranslucentStatus(Activity activity, boolean on) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    public static int getChildAtParent(ViewGroup parent, View child) {
        if (parent == null || child == null) {
            return -1;
        }
        for (int i = 0; i < parent.getChildCount(); i++) {
            if (parent.getChildAt(i).equals(child)) {
                return i;
            }
        }
        return -1;

    }

}
